﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using System.Data;

namespace EnterpriseFund.Controllers
{
        [OutputCache(Duration = 0, VaryByParam = "none")]
    public class ReturnDlgController : AdminBaseController
    {
        //
        // GET: /ReturnDlg/

        public ActionResult Index(string tableName,string id,string feq)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FTableName");
            dt.Columns.Add("FSeq");
            dt.Columns.Add("FNodeName");
            dt.Columns.Add("TableKey");
            dt.Columns.Add("TableFStatus");
            dt.TableName = "workFlow";
            dt.ReadXml(Server.MapPath("~") + "\\WorkFlow.xml");

            DataRow[] dr = dt.Select("FTableName='" + tableName + "'");

            DataTable nDt = dr.CopyToDataTable();

            DataRow[] nDr = nDt.Select("FSeq<" + feq);

            DataTable showDt = nDr.CopyToDataTable();

            ViewData["Showdata"] = showDt;

            ViewData["tableName"] = tableName;
            ViewData["id"] = id;
            ViewData["feq"] = feq;
            return View("Index");
        }

        public ActionResult Save()
        {
            string tableName = Fetch.Post("hidtableName");
            string no = Fetch.Post("hidid");
            string feq = Fetch.Post("hidfeq");
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("FTableName");
                dt.Columns.Add("FSeq");
                dt.Columns.Add("FNodeName");
                dt.Columns.Add("TableKey");
                dt.Columns.Add("TableFStatus");
                dt.TableName = "workFlow";
                dt.ReadXml(Server.MapPath("~") + "\\WorkFlow.xml");

                DataRow[] dr = dt.Select("FTableName='" + tableName + "'");

                DataTable nDt = dr.CopyToDataTable();

                string c = DevExpress.Web.Mvc.ComboBoxExtension.GetValue<string>("FSeq");

                DataRow[] nDr = nDt.Select("FSeq=" + c);

                if (nDr.Count() > 0)
                {
                    BLL.WorkFlowProcess workFlowProcessBLL = new BLL.WorkFlowProcess();
                    Model.WorkFlowProcess wpModel = new Model.WorkFlowProcess();
                    wpModel.FEntityNo = GlobalConstant.ENTITYNO;
                    wpModel.FDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                    wpModel.FEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                    wpModel.FInsIP = GenerateUtil.GetClientIPv4Address();
                    wpModel.FNote = Fetch.Post("FNote");
                    wpModel.FProcDate = DateTime.Now;
                    wpModel.FSeq = int.Parse(feq);
                    wpModel.FSourceNo = no;
                    wpModel.FTableName = tableName;
                    Model.WorkFlowToDo wtModel = new Model.WorkFlowToDo();
                    wtModel.FEntityNo = GlobalConstant.ENTITYNO;
                    wtModel.FSourceNo = no;
                    wtModel.FSubmitDate = DateTime.Now;
                    wtModel.FSubmitDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                    wtModel.FSubmitEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                    wtModel.FSubmitIP = GenerateUtil.GetClientIPv4Address();
                    wtModel.FTableName = tableName;
                    wpModel.FStatus = "R";
                    wtModel.FStatus = "T";
                    wtModel.FSeq = int.Parse(c);
                    workFlowProcessBLL.Add(wpModel, wtModel);

                    workFlowProcessBLL.UpdateR(no, nDr[0]["TableKey"].ToString(), nDr[0]["TableFStatus"].ToString(), tableName);
                    try
                    {
                        WorkFlow(tableName, c, no, "return", wpModel.FNote);
                        if (tableName == "t_AllowanceIssue")
                        {
                            Model.AllowanceIssueEntry model = new Model.AllowanceIssueEntry();
                            model.FStatus = "0";
                            model.FIssueID = no;
                            BLL.AllowanceIssueEntry AllowanceIssueEntryBLL = new BLL.AllowanceIssueEntry();
                            AllowanceIssueEntryBLL.Return(model);
                        }

                        if (tableName == "t_TaxAuditForm")
                        {
                            Model.TaxAuditForm model = new Model.TaxAuditForm();
                            model.FStatus = "0";
                            model.FTaxAuditId = no;
                            model.FIsReturn = "N";
                            BLL.TaxAuditForm taxAuditFormBLL = new BLL.TaxAuditForm();
                            taxAuditFormBLL.Return(model);
                        }
                    }
                    catch
                    {
 
                    }
                    
                    ViewData["jsAlert"] = "操作成功";
                }
                else
                {
                    ViewData["jsAlert"] = "未找到选择流程，请关闭页面后重试！";
                }

            }
            catch (Exception ex)
            {
                ViewData["jsAlert"] = ex.Message;
            }

            return View("index");
        }

    }
}
